package top.yeyusmile.controller;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.yeyusmile.common.MyTemplate;
import top.yeyusmile.common.RobotConfig;
import top.yeyusmile.common.TypeConstant;
import top.yeyusmile.pojo.MyQQMessage;
import top.yeyusmile.pojo.ReqResult;
import top.yeyusmile.service.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/15 23:42
 */
@Slf4j
@RequestMapping("/dispatcher")
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class DispathcherController {


    @Autowired
    private RobotConfig robotConfig;

    //存旧的信息，方便撤回
    private List<MyQQMessage> saveMsg = new LinkedList<MyQQMessage>();

    private static final int CAPACITY = 2000;//存2000条 防撤回用


    @Autowired
    private DianGeService dianGeService;

    @Autowired
    private SendMsgService sendMsgService;

    @Autowired
    private ConvenientService convenientService;

    @Autowired
    private QbangService qbangService;

    @Autowired
    private BlackWordService blackWordService;

    @Autowired
    private OtherService otherService;


    @PostMapping
    public ReqResult newMsg(@RequestBody MyQQMessage message/*, HttpServletRequest request*/) throws Exception {

        Thread.sleep(500);

        if (saveMsg.size() < CAPACITY) {
            saveMsg.add(message);
        } else {
            saveMsg.remove(0);
            saveMsg.add(message);
        }

        //测试用
        //if (3 > 2) return new ReqResult(200);
        //if (message.getMqType() != 2006 || message.getMqType() != 2007)
        // return new ReqResult(100);

        message.setMqMsg(URLDecoder.decode(message.getMqMsg(), "utf-8"));
        String msg = message.getMqMsg();

        if (blackWordService.isBlackWord(message)) return new ReqResult(-1);


        switch (message.getMqType()) {
            case TypeConstant.MSGTYPE_NODEFINE://消息类型_未定义
                break;
            case TypeConstant.MSGTYPE_FRIEDN://消息类型_好友
            case TypeConstant.MSGTYPE_GROUP://消息类型_群


                if ("菜单".equals(msg) || "功能".equals(msg)) {
                    sendMsgService.sendMsg(GetMenu(), message);
                    break;
                }

                if ("博客".equals(msg)) {
                    sendMsgService.sendMsg("https://yeyusmile.top/", message);
                    break;
                }

                if ("插件开源".equals(msg)) {
                    sendMsgService.sendMsg("https://gitee.com/yang10560/my-qqrobot-sdk", message);
                    break;
                }

                if ("加入交流群".equals(msg)) {
                    sendMsgService.sendMsg("873388675", message);
                    break;
                }

                if (message != null && msg != null) {

                    if (msg.contains("ip探针") && msg.length() < 20) {
                        otherService.ip(message);
                        break;
                    }


                    if (msg.contains("点歌")) {
                        dianGeService.diange(message);//点歌
                        break;
                    }

                    if (msg.contains("FlashPic=") && robotConfig.getFlashPicSwitch()) {
                        sendMsgService.sendMsg("[@" + message.getMqFromqq() + "] 闪照:" + msg.replaceAll("FlashPic=", "Pic="),
                                message);
                        break;
                    }//防闪照

                    if (msg.contains("历史上的今天")) {
                        // convenientService.historyToday(message);//历史上的今天
                        break;
                    }

                    if (msg.equals("60s")) {
                        // convenientService.SixtyReadWorld(message); //60s读懂世界
                        break;
                    }

                    if (msg.equals("随机语录")) {
                        convenientService.quotation(message);
                        break;
                    }

                    if (msg.equals("舔狗日记")) {
                        convenientService.diary(message);
                        break;
                    }

                    if (msg.equals("看美女") || "美女".equals(msg)) {
                        convenientService.lookGirl(message);
                        break;
                    }

                    if (msg.equals("动漫") || "二次元".equals(msg)) {
                        convenientService.lookAnime(message);
                        break;
                    }

                    if (msg.equals("绕口令")) {
                        convenientService.raokouling(message);
                        break;
                    }

                    if (msg.equals("笑话")) {
                        convenientService.joke(message);
                        break;
                    }

                    if (msg.equals("段子")) {
                        convenientService.duanzi(message);
                        break;
                    }

                    if (msg.contains("QQ凶吉")) {
                        convenientService.qqLucky(msg.replaceAll("QQ凶吉", ""), message);
                        break;
                    }

                    if (msg.contains("天气") && msg.length() < 10) {
                        convenientService.weather(msg.replaceAll("天气", ""), message);
                        break;
                    }

                    if (msg.contains("禁言") && msg.length() > 5) {
                        if (robotConfig.getOwners().contains(message.getMqFromqq())) {
                            sendMsgService.shutUp(message, msg.replaceAll("禁言", ""), 300);
                        } else {
                            sendMsgService.sendMsg("权限不足", message);
                        }
                        break;

                    }

                    if (msg.contains("解禁") && msg.length() > 5) {
                        if (robotConfig.getOwners().contains(message.getMqFromqq())) {
                            sendMsgService.shutUp(message, msg.replaceAll("解禁", ""), 0);
                        } else {
                            sendMsgService.sendMsg("权限不足", message);
                        }
                        break;
                    }

                    //处理事件
                    if (msg.contains("查询")) {
                        if (message.getMqMsg().matches("查询[1-9][0-9]{3,9}")) {
                            qbangService.query(msg.replaceAll("查询", ""), message, 0);
                        }
                        break;
                    }

                    if (msg.contains("绑查Q")) {
                        qbangService.query(msg.replaceAll("绑查Q", ""), message, 1);
                        break;
                    }
                    if (msg.contains("查微博")) {
                        qbangService.query(msg.replaceAll("查微博", ""), message, 3);
                        break;
                    }
                    if (msg.contains("绑查微博")) {
                        qbangService.query(msg.replaceAll("绑查微博", ""), message, 4);
                        break;
                    }

                    if (msg.contains("LOL查Q")) {
                        qbangService.query(msg.replaceAll("LOL查Q", ""), message, 2);
                        break;
                    }

                }
                break;
            case TypeConstant.MSGTYPE_GROUP_RECALL_MSG://消息类型_群撤回
            case TypeConstant.MSGTYPE_FREND_RECALL_MSG://消息类型_好友撤回
                System.out.println(message);
                if (!robotConfig.getGoBackcSwitch()) return new ReqResult(-1);
                for (MyQQMessage sMsg : saveMsg) {
                    if (sMsg.getMqMsgseq().equals(message.getMqMsgseq()) &&
                            sMsg.getMqMsgid().equals(message.getMqMsgid())
                            && !message.getMqFromqq().equals(robotConfig.getRobotQQ())) {
                        sendMsgService.sendMsg("[@" + message.getMqFromqq() + "] 撤回:" + sMsg.getMqMsg(), sMsg);
                        break;
                    }
                }//防撤回
                break;

            case TypeConstant.MSGTYPE_BE_INVITE_TO_GROUP:
                System.out.println("某人申请加群：" + message);
                if (robotConfig.containGid(message.getMqFromid())) {
                    sendMsgService.handleGroupInvite(TypeConstant.DEAL_AGREE_CONFIRM, message);
                }//同意加群
                break;

            case TypeConstant.MSGTYPE_BE_ADD_FRIEND:
                System.out.println("被申请好友：" + message);
                if (robotConfig.getAddFrendSwitch()) {
                    sendMsgService.handleFriendEvent(TypeConstant.DEAL_AGREE_CONFIRM, message);
                }//同意好友
                break;
            case TypeConstant.MSGTYPE_ME_BE_INVITE_GROUP:
                System.out.println("我被邀请进群" + message.getMqFromqq());
                if (robotConfig.getAddGroupSwitch()) {
                    sendMsgService.handleGroupInvite(TypeConstant.DEAL_AGREE_CONFIRM, message);
                }

                break;
            case TypeConstant.MSGTYPE_GROUP_FILE_RECEIVE:

                log.info("群文件接收,{}", message);

                otherService.alertGroupFileReceive(message);

                break;
            case TypeConstant.MSGTYPE_ONE_QUIT_GROUP:
            case TypeConstant.MSGTYPE_ONE_BE_QUIT:
                log.info("退群，移人,{}", message);
                otherService.alterQuitGroup(message);
            case TypeConstant.MSGTYPE_GROUPCARD_CHANGE:
                log.info("名片变动,{}", message);
                otherService.alterGroupChange(message);

        }
        return new ReqResult(1);

    }


    private String GetMenu() {
        return "------菜单-功能------\n" +
                "[点歌+歌名或歌手]  说明(如：点歌周杰伦稻香)\n" +
                // "[60s] 说明(60秒读懂世界，每天7点播报)\n" +
                "[随机语录]  [舔狗日记] \n" +//[历史上的今天]
                "[看美女] [二次元] [绕口令]  \n" +
                "[某地 + 天气] [QQ凶吉 + ＱＱ号]" +
                "[查询 + ＱＱ号] [绑查Q + 手机号] [查微博 + 微博ID]\n" +
                "[绑查微博 + 手机号] [LOL查Q + 昵称]\n" +
                "[加入交流群] [博客] [插件开源] [ip探针+伪装标题]\n" +
                "---------------------\n" +
                "默认开启功能：\n" +
                "群闪照破解;群消息防撤;自动同意好友;自动同意加群\n" +
                "群申请自动同意;违禁词检测;退群,名片更改提醒\n" +
                "------夜雨小屋------";
    }


}
